package webModule
{
	import com.greensock.TweenMax;
	import com.hydrotik.queueloader.QueueLoader;
	import com.hydrotik.queueloader.QueueLoaderEvent;
	import com.touchX.display.AbstractDisplay;
	import com.touchX.display.FreedomDisplayObject;
	import com.touchX.display.LoaderManager;
	import flash.geom.Rectangle;
	import flash.events.Event;
	
	import webModule.product.Product3DViewer;
	import webModule.product.ProductEvent;
	import webModule.product.ProductModule;
	
	public class Products extends AbstractDisplay
	{
		private var _productModuleList:Vector.<ProductModule>;
		private var _loader:QueueLoader;
		private var	_product3DView:Product3DViewer;
		private var _3dViewShowing:Boolean=true;
		public function Products(listUrl:String)
		{
			super();
			LoaderManager.getInstance().createLoader();
			_loader=LoaderManager.getInstance().loader;
			
			_loader.addItem(listUrl,null,{title:"XML:ProductList",target:this});
			if(!_loader.isLoading()) _loader.execute();
		}
		protected override function resize(event:Event=null):void{
			super.resize(event);
			if(_product3DView){
				TweenMax.to(_product3DView,.5,{x:(stage.stageWidth-_product3DView.width)/2});
			}
		}
		public function onItemComplete(evt:QueueLoaderEvent):void{
			if(evt.title=="XML:ProductList"){
				_productModuleList=new Vector.<ProductModule>();
				var productList:XML = XML(evt.content);
				for(var i:int=0;i<productList.child("product").length();i++){
					var module:ProductModule=new ProductModule(XML(productList.child("product")[i]),this);
					module.addEventListener(ProductEvent.SHOW_PRODUCT,showProduct3Dview);
					_productModuleList.push(module);
				}
			}
		}
		private function showProduct3Dview(evt:ProductEvent):void{
			if(!_product3DView){
				_product3DView=new Product3DViewer();
				_product3DView.x=(stage.stageWidth-_product3DView.width)/2;
				_product3DView.y=50;
				
				thumbnailCloseLeft();
				addChildAt(_product3DView,0);
				_product3DView.addEventListener(Product3DViewer.VIEW3D_REMOVED,removeView3D);
				TweenMax.from(_product3DView,.5,{y:-500});
			}
			
			_product3DView.showProduct(evt.module.module);
		}
		private function thumbnailCloseLeft():void{
			for(var i:int=0;i<_productModuleList.length;i++){
				_productModuleList[i].view.spreadToRectangle(new Rectangle(0,10,10,stage.stageHeight-50));
				_productModuleList[i].view.closeLeft=true;
			}
		}
		private function view3dRemoved(evt:Event):void{
			TweenMax.to(_product3DView,.5,{y:-500,onComplete:removeView3D});
		}
		private function removeView3D(evt:Event):void{
			removeChild(_product3DView);
			_product3DView=null;
			for(var i:int=0;i<_productModuleList.length;i++){
				_productModuleList[i].view.closeLeft=false;
				_productModuleList[i].view.spreadToRectangle(new Rectangle(0,10,stage.stageWidth,stage.stageHeight-50));
				
			}
		}
	}
}